package com.xinsoft.controller.outsourcing;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xinsoft.common.ApprovedParam;
import com.xinsoft.common.Response;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.*;
import com.xinsoft.entity.vo.ScmOutsourcingContractDetailVo;
import com.xinsoft.entity.vo.ScmOutsourcingContractPageVo;
import com.xinsoft.service.ApproveService;
import com.xinsoft.service.ScmOutsourcingContractService;
import com.xinsoft.utils.ExcelUtils;
import com.xinsoft.utils.TokenUtil;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 委外合同
 * @folder 云供应链/委外管理/委外合同
 */
@RestController
@RequestMapping("/scm/outsourcingContract")
public class ScmOutsourcingContractController {

    @Autowired
    private ScmOutsourcingContractService outsourcingContractService;
    @Autowired
    private ApproveService approveService;

    /**
     * 分页
     */
    @PostMapping("/pageList")
    public Response<IPage<ScmOutsourcingContractPageVo>> pageList(@RequestBody ScmOutsourcingContractParam param) {
        param.setCurrentUserId(TokenUtil.getUserId());
        return Response.succeed(outsourcingContractService.findPageListByParam(param));
    }

    // @PostMapping("/pageList1")
    // public ScmOutsourcingContractPageVo pageList1(@RequestBody ScmOutsourcingContractParam param) {
    //     param.setCurrentUserId(TokenUtil.getUserId());
    //     return null;
    // }

    /**
     * 暂存
     */
    @PostMapping("/addOrUpdate")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> addOrUpdate(@RequestBody ScmOutsourcingContractDTO dto) {
        ValidatorUtils.validateEntity(dto);
        outsourcingContractService.tempSave(dto);
        return Response.succeed();
    }

    /**
     * 提交审批
     */
    @PostMapping("/submitApprove")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> submitApprove(@RequestBody ScmOutsourcingContractDTO dto) {
        Integer id = outsourcingContractService.tempSave(dto);
        ApproveDTO approveDTO = new ApproveDTO();
        approveDTO.setSourceId(id);
        approveDTO.setSourceType(Const.ApproveType.WWHT);
        approveDTO.setSourceTypeName(Const.ApproveTypeName.WWHT);
        return approveService.submitApprove(approveDTO);
    }

    /**
     * 审批
     */
    @PostMapping("/approved")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> approved(@Validated @RequestBody ApprovedParam param) {
        Response<?> approved = approveService.approved(param, Const.ApproveType.WWHT);
        // TODO 审批通过生成委外投料
        return approved;
    }

    /**
     * 审批流程
     */
    @GetMapping("/approveHis")
    public Response<?> approveHis(@RequestParam(value = "id") Integer id){
        return Response.succeed(approveService.approvalHistory(id, Const.ApproveType.WWHT));
    }

    /**
     * 获取详情
     *
     * @param id 销售订单id
     * @return {@link Response}<{@link ?}>
     */
    @GetMapping("/detail")
    public Response<ScmOutsourcingContractDetailVo> detail(@RequestParam(value = "id", defaultValue = "") Integer id) {
        ScmOutsourcingContractDetailVo vo = outsourcingContractService.detail(id);
        // // 审批记录
        // vo.setScmApproveHisList(approveService.approvalHistory(id, Const.ApproveType.WWHT));
        return Response.succeed(vo);
    }

    /**
     * 删除
     */
    @PostMapping("/del")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> del(@RequestBody List<Integer> ids) {
        outsourcingContractService.del(ids.stream().distinct().collect(Collectors.toList()));
        return Response.succeed();
    }

    /**
     * 关闭
     * @param ids 销售订单ids
     * @return {@link Response}<{@link ?}>
     */
    @PostMapping("/close")
    public Response<?> close(@RequestBody List<Integer> ids) {
        outsourcingContractService.close(ids);
        return Response.succeed();
    }

    /**
     * 开启（反关闭）
     * @param ids 销售订单ids
     * @return {@link Response}<{@link ?}>
     */
    @PostMapping("/reverseClose")
    public Response<?> reverseClose(@RequestBody List<Integer> ids) {
        outsourcingContractService.reverseClose(ids);
        return Response.succeed();
    }

    /**
     * 行关闭
     */
    @PostMapping("/lineClose")
    public Response<?> lineClose(@RequestBody ScmOutsourcingContractLineClose dto) {
        ValidatorUtils.validateEntity(dto);
        outsourcingContractService.lineClose(dto);
        return Response.succeed();
    }

    /**
     * 导出
     */
    @PostMapping("/export")
    public Response<?> export(@RequestBody @ApiParam("查询参数") ScmOutsourcingContractParam param, HttpServletResponse response) throws Exception {
        List<ScmOutsourcingContractPageVo> listByParam = outsourcingContractService.findListByParam(param);
        ExcelUtils.exportByEasypoi(listByParam, ScmOutsourcingContractPageVo.class, "委外合同", "委外合同", response, "委外合同");
        return Response.succeed();
    }
}
